<?php

include_once("config.php");
/*
* First, we define some PHP functions to expose via
* XML-RPC. Any functions that will be called by a
* XML-RPC client need to take three parameters:
* The first parameter passed is the name of the
* XML-RPC method called, the second is an array
* Containing the parameters sent by the client, and
* The third is any data sent in the app_data
* parameter of the xmlrpc_server_call_method()
* function (see below).
*/


function send($method_name, $params, $app_data) {

  $note = $params[0];
  logaa($note);
  return "Notebooklet server " . NOTEBOOKLET_VERSION . " says: Note saved. :-)";
}



function send_note($method_name, $params, $app_data) {
  $status_not_authenticated = -1;
  $status_invalid_content   = -2;
  $status_invalid_header    = -3;
  $status_internal_error    = -4;
  $status_ok                = 0;
  
  $header_min_length        = 3;
  $content_min_length       = 2;

  $username = $params[0]['username'];
  $password = $params[0]['password'];
  $header   = $params[0]['header'];
  $content  = $params[0]['content'];

  # Validity checks
  if( strlen($header) < $header_min_length ) {
      return $status_invalid_header;
  }
  if( strlen($content) < $content_min_length ) {
      return $status_invalid_content;
  }

  # Checking authentication
  if( $username == NOTEBOOKLET_USERNAME && $password == NOTEBOOKLET_PASSWORD) {
      logaa("Autenthication ok");
      $saving_ok = save_data($content, $header);
      if( $saving_ok ) {
          return $status_ok;
      }
      return $status_internal_error;
  }

  # Not authenticated
  return $status_not_authenticated;
}

function logaa($teksti) {
    $hanska = fopen("logi.txt", 'a');
    fwrite($hanska, $teksti."\n");
    fclose($hanska);
}

function save_data($content, $header) {
    # $dbh = new PDO('mysql:host='.MYSQL_HOST.';dbname='.MYSQL_DB,MYSQL_USER,MYSQL_PASS);
    # $insert_sql = $dbh->prepare('INSERT INTO ' . NOTEBOOKLET_DATATABLE . '(content, header) values (:content, :header)');
    # $insert_sql->bindParam(":header", $header);
    # $insert_sql->bindParam(":content", $content);

    logaa("Starting data saving");

    $db = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS) or die("Can't login.");
    mysql_select_db(MYSQL_DB, $db) or die("Can't connect to database.");

    $sql = trim('INSERT INTO ' . NOTEBOOKLET_DATATABLE . ' (header, content) VALUES ('.$header.','.$content.')');
    try {
        $result = mysql_query($sql);
    }
    catch(Exception $e) {
        logaa("Exception! " . $result . ': ' . $e);
    }

    logaa("Data saving done: " . $result);

    return $result;
    # return $sql->execute();
}

function about_func($method_name, $params, $app_data)
{
return "Notebooklet XML-RPC PHP Server (version ". NOTEBOOKLET_VERSION . ") - Made by Heikki Salo, heikki.ao.salo@iki.fi";
}
/*
* This creates a server and sets a handle for the
* server in the variable $xmlrpc_server
*/
$xmlrpc_server = xmlrpc_server_create();
/*
* xmlrpc_server_register_method() registers a PHP
* function as an XML-RPC method. It takes three
* parameters:
* The first is the handle of a server created with
* xmlrpc_server_create(), the second is the name to
* register the server under (this is what needs to
* be in the <methodName> of a request for this
* method), and the third is the name of the PHP
* function to register.
*/
xmlrpc_server_register_method($xmlrpc_server, "about", "about_func");
xmlrpc_server_register_method($xmlrpc_server, "sendnote", "send_note");
xmlrpc_server_register_method($xmlrpc_server, "send", "send");
/*
* When an XML-RPC request is sent to this script, it
* can be found in the raw post data.
*/
$request_xml = $HTTP_RAW_POST_DATA;
/*
* The xmlrpc_server_call_method() sends a request to
* the server and returns the response XML. In this case,
* it sends the raw post data we got before. It requires
* 3 arguments:
* The first is the handle of a server created with
* xmlrpc_server_create(), the second is a string containing
* an XML-RPC request, and the third is for application data.
* Whatever is passed into the third parameter of this function
* is passed as the third paramater of the PHP function that the
* request is asking for.
*/
$response = xmlrpc_server_call_method($xmlrpc_server, $request_xml, '');
// Now we print the response for the client to read.
print $response;
/*
* This method frees the resources for the server specified
* It takes one argument, a handle of a server created with
* xmlrpc_server_create().
*/
xmlrpc_server_destroy($xmlrpc_server);
?>
